package com.spgf.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.entities.CategoriaModel;
import com.spgf.model.entities.LancamentoModel;
import com.spgf.model.rn.CategoriaRN;
import com.spgf.model.rn.LancamentoRN;
import com.spgf.model.utils.ConstantesSPGF;
import com.spgf.view.utils.AdapterListViewDespesas;
import com.spgf.view.utils.DialogManutencao;

public class PendenciasActivity extends PadraoActivity {

	private List<LancamentoModel>	listaModel;

	private ListView				listview;

	@SuppressLint("SimpleDateFormat")
	public void atualizarLista() throws NegocioException {

		listaModel = null;
		listview.setAdapter(new AdapterListViewDespesas(this, getListaModel()));
	}

	@Override
	protected void construirTela(Bundle savedInstanceState) throws NegocioException {
		setContentView(R.layout.activity_pendencias);

		listview = (ListView) findViewById(R.id.lst_resumo_categoria_pendentes);
		listview.setScrollbarFadingEnabled(true);
		listview.setOnItemLongClickListener(this);

		atualizarLista();
	}

	protected boolean executarLongClick(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException {

		if (pParent.getId() == R.id.lst_resumo_categoria_pendentes) {

			new DialogManutencao(this, getListaModel().get(pPosition)).show();
			return true;
		}

		return false;

	}

	public List<LancamentoModel> getListaModel() throws NegocioException {

		if (listaModel == null) {

			CategoriaModel lCategoria = new CategoriaModel();
			lCategoria.setCategoriaSuperior(new CategoriaModel(ConstantesSPGF.CODIGO_CATEGORIA_PENDENTE_AVALIACAO));

			List<CategoriaModel> lListCategorias = new CategoriaRN(this).obterPorFiltro(lCategoria);

			listaModel = new ArrayList<LancamentoModel>();

			LancamentoModel lancamento = new LancamentoModel();
			lancamento.setData(Calendar.getInstance().getTime());

			for (CategoriaModel lCategoriaTemp : lListCategorias) {
				lancamento.setCategoria(lCategoriaTemp);
				listaModel.addAll(new LancamentoRN(this).obterLancamentosPorMes(lancamento));
			}

		}

		return listaModel;
	}
}
